package com.meixiang.activity.account;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.myShopper.OrderDetailActivity;
import com.meixiang.adapter.LogisticsMessageAdapter;
import com.meixiang.entity.account.AccountMessageList;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 物流消息
 * author：wangzuzhen on 2016/5/6 0006 10:33.
 */
public class LogisticsMessageActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @Bind(R.id.swipe_target)
    RecyclerView mSwipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout mRefresh;

    private LogisticsMessageAdapter mAdapter;

    private Activity mActivity;
    private int mPageNo, totalPage; //当前页和每页显示的条数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_logistics_message);
        ButterKnife.bind(this);
        mActivity = LogisticsMessageActivity.this;
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle("物流消息");
    }

    @Override
    protected void initView() {
        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
        mAdapter = new LogisticsMessageAdapter(context);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
        mSwipeTarget.setLayoutManager(layoutManager);
        mSwipeTarget.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new LogisticsMessageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, final int position) {
                //预约详情
                HttpParams params = new HttpParams();
                params.put("messageId", mAdapter.mList.get(position).getMessageId());
                HttpUtils.post(Config.SERVER_URL + "sys/pushMessage/changeStatus", params, new HttpCallBack(LogisticsMessageActivity.this) {
                    @Override
                    public void onSucceed(JSONObject result, String code, String message) {
                        Intent intent = new Intent(mActivity, OrderDetailActivity.class);
                        intent.putExtra("orderId", mAdapter.mList.get(position).getOrderId());
                        startActivity(intent);
                    }

                    @Override
                    public void onError(String code, String message) {
                        Tool.showTextToast(context, message);
                    }
                });

            }
        });


    }

    @Override
    protected void setData() {
        status.showLoading();
        getMessageData(1, true);
    }

    private void getMessageData(int pageNo, final boolean isPull) {
        HttpParams params = new HttpParams();
        params.put("type", "1");//1表示查询物流信息 0表示查询服务信息
        HttpUtils.post(Config.SERVER_URL + "sys/pushMessage/orderShipMessage", params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                onComplet();
                if (isPull) {
                    mAdapter.clearData();
                }
                mPageNo = result.optInt("pageNo");
                totalPage = result.optInt("totalPage");
                String imgServer = result.optString("imgServer");
                List<AccountMessageList> mList = AbJsonUtil.fromJson(result.optJSONArray("list").toString(), new TypeToken<List<AccountMessageList>>() {
                });
                if (mList.size() > 0) {
                    status.removeView();
                    mAdapter.addAll(mList, imgServer);
                } else {
                    status.showNoData("暂无数据");
                }
            }

            @Override
            public void onError(String code, String message) {
                onComplet();
                status.showNoNewWork(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getMessageData(1, isPull);
                    }
                });

                if (message.equals("暂无数据")) {
                    status.showNoData("暂无数据");
                } else {
                    status.showNoData(message, "重新加载", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            status.showLoading();
                            getMessageData(1, isPull);
                        }
                    });
                }
            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                super.onAfter(isFromCache, jsonObject, call, response, e);
                onComplet();
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    public void onComplet() {
        if (mRefresh != null) {
            if (mRefresh.isRefreshing()) {
                mRefresh.setRefreshing(false);
            } else {
                mRefresh.setLoadingMore(false);
            }
        }
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        if (mPageNo < totalPage) {
            getMessageData(mPageNo + 1, false);
        } else {
            AbToastUtil.showToast(activity, "已无更多数据");
            onComplet();
        }
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        getMessageData(1, true);
    }

}
